package cn.yikaojiuguo.student.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cfan_yang on 2015/12/14.
 */
public abstract class SimpleBaseAdapter<T> extends BaseAdapter{

    protected List<T> mList;
    protected Context mContext;

    public SimpleBaseAdapter(Context context, List<T> list) {
        this.mContext = context;
        this.mList = list;
    }

    public SimpleBaseAdapter(Context context) {
        this.mContext = context;
        this.mList = new ArrayList<T>();
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public T getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public abstract View getView(int position, View convertView, ViewGroup parent);

    public void add(List<T> list) {
        this.mList.addAll(list);
        notifyDataSetChanged();
    }

    /**
     * 分页时调用，添加第一页数据
     */
    public void addFirstPageData(List<T> list) {
        if (list == null) {
            return;
        }
        mList.clear();
        add(list);
    }

    /**
     * 分页时调用，添加第一页之后的数据
     */
    public void addOtherPageData(List<T> list) {
        if (list == null || list.isEmpty()) {
            return;
        }
        add(list);
    }

    /**
     * 删除指定元素
     */
    public void remove(int index) {
        if (mList == null || index < 0 || index >= mList.size()) {
            return;
        } else {
            mList.remove(index);
            notifyDataSetChanged();
        }
    }

    /**
     * 清空数据
     */
    public void removerAll() {
        if(mList!=null) {
            mList.clear();
            notifyDataSetChanged();
        }
    }
}
